<template>
  <div class="app-container">
    <div v-loading="loading" class="pwd-form-wrapper">
      <!-- 添加或修改密码设置对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="是否单一登录" prop="singleLogin">
          <el-switch v-model="form.singleLogin" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item v-if="form.singleLogin==1" label="踢出类型" prop="singleLogin">
          <el-radio-group v-model="form.kickType">
            <el-radio :label="0">后登录踢出先登录</el-radio>
            <el-radio :label="1">已登录禁止再登录</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-divider></el-divider>
        <el-form-item label="是否密码强度限制" prop="strengthLimit">
          <el-switch v-model="form.strengthLimit" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item v-if="form.strengthLimit==1" label="是否需要最小长度限制" prop="needLength">
          <el-switch v-model="form.needLength" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item v-if="form.needLength==1 && form.strengthLimit==1" label="最小长度" prop="minLength">
          <el-input-number v-model="form.minLength" :min="8" :step="1" :max="64"></el-input-number>
        </el-form-item>
        <el-form-item v-if="form.strengthLimit==1" label="包含数字" prop="needDigit">
          <el-switch v-model="form.needDigit" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item v-if="form.strengthLimit==1" label="包含小写字母" prop="needLowerCase">
          <el-switch v-model="form.needLowerCase" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item v-if="form.strengthLimit==1" label="包含大写字母" prop="needUpperCase">
          <el-switch v-model="form.needUpperCase" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item v-if="form.strengthLimit==1" label="包含特殊字符" prop="needSpecialCharacter">
          <el-switch v-model="form.needSpecialCharacter" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item v-if="form.strengthLimit==1" label="不包含用户名" prop="notUserName">
          <el-switch v-model="form.notUserName" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-divider></el-divider>
        <el-form-item label="初始密码强制修改" prop="forceChange">
          <el-switch v-model="form.forceChange" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="限制密码错误次数" prop="limitFailedCount">
          <el-input-number v-model="form.limitFailedCount" :step="1" :min="0" :max="10"></el-input-number>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">保 存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getSysPassword, delSysPassword, updateSysPassword } from '@/api/system/sys-password'

export default {
  name: 'SysPassword',
  data() {
    return {
      // 遮罩层
      loading: true,
      submitLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 密码设置表格数据
      sysPasswordList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        id: null,
        singleLogin: 0,
        kickType: 0,
        strengthLimit: 0,
        needLength: 0,
        minLength: 8,
        needDigit: 0,
        needLowerCase: 0,
        needUpperCase: 0,
        needSpecialCharacter: 0,
        notUserName: 0,
        forceChange: 0,
        limitFailedCount: 5,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      },
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询密码设置列表 */
    getList(showLoading = true) {
      if (showLoading) {
        this.loading = true
      }
      getSysPassword().then(response => {
        this.form = response.data
        this.$store.commit('SET_SYS_PASSWORD_CONFIG_DATA', { ...response.data })
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        singleLogin: 0,
        kickType: 0,
        strengthLimit: 0,
        needLength: 0,
        minLength: 8,
        needDigit: 0,
        needLowerCase: 0,
        needUpperCase: 0,
        needSpecialCharacter: 0,
        notUserName: 0,
        forceChange: 0,
        limitFailedCount: 5,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.submitLoading = false
      this.title = '添加密码设置'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSysPassword(id).then(response => {
        this.form = response.data
        this.open = true
        this.submitLoading = false
        this.title = '修改密码设置'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.submitLoading = true
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateSysPassword(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.submitLoading = false
            this.getList(false)
          }).catch(err => {
            this.submitLoading = false
            console.error(err)
          })
        }
      })
    },
    /** 批量删除 */
    handleDeleteAll() {
      const ids = this.ids
      if (this.ids.length > 0) {
        this.$modal.confirm('是否确认删除密码设置编号为"' + ids + '"的数据项？').then(() => {
          return delSysPassword(ids).then(() => {
            this.getList()
            this.$modal.msgSuccess('删除成功')
          }).catch(() => {
          })
        })
      } else {
        this.$message.warning('未选中数据')
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除密码设置编号为"' + ids + '"的数据项？').then(function() {
        return delSysPassword(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sysPassword/export', {
        ...this.queryParams
      }, `sysPassword_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  .pwd-form-wrapper {
    padding: 0 10%;

    .dialog-footer {
      padding-left: 180px;
    }
  }
  .el-input-number {
    width: 200px;
  }
}
</style>
